//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.xtl.config;

import java.util.Iterator;
import java.util.Map;
import org.pentaho.di.core.JndiUtil;
import org.pentaho.di.core.KettleVariablesList;
import org.pentaho.di.core.auth.AuthenticationConsumerPluginType;
import org.pentaho.di.core.auth.AuthenticationProviderPluginType;
import org.pentaho.di.core.compress.CompressionPluginType;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettlePluginException;
import org.pentaho.di.core.lifecycle.KettleLifecycleSupport;
import org.pentaho.di.core.logging.ConsoleLoggingEventListener;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.logging.LogTablePluginType;
import org.pentaho.di.core.plugins.CartePluginType;
import org.pentaho.di.core.plugins.DatabasePluginType;
import org.pentaho.di.core.plugins.ImportRulePluginType;
import org.pentaho.di.core.plugins.JobEntryPluginType;
import org.pentaho.di.core.plugins.KettleLifecyclePluginType;
import org.pentaho.di.core.plugins.LifecyclePluginType;
import org.pentaho.di.core.plugins.PartitionerPluginType;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.RepositoryPluginType;
import org.pentaho.di.core.plugins.StepPluginType;
import org.pentaho.di.core.variables.Variables;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.step.RowDistributionPluginType;
import org.yaukie.xtl.cons.Constant;

public class KettleInit {
    private static Class<?> PKG = Constant.class;
    private static Boolean initialized;

    public KettleInit() {
    }

    public static void init() throws KettleException {
        init(true);
    }

    public static void init(boolean simpleJndi) throws KettleException {
        if (initialized == null) {
            KettleLogStore.init();
            KettleLogStore.getAppender().addLoggingEventListener(new ConsoleLoggingEventListener());
            if (simpleJndi) {
                JndiUtil.initJNDI();
            }

            PluginRegistry.addPluginType(RowDistributionPluginType.getInstance());
            PluginRegistry.addPluginType(LogTablePluginType.getInstance());
            PluginRegistry.addPluginType(CartePluginType.getInstance());
            PluginRegistry.addPluginType(CompressionPluginType.getInstance());
            PluginRegistry.addPluginType(AuthenticationProviderPluginType.getInstance());
            PluginRegistry.addPluginType(AuthenticationConsumerPluginType.getInstance());
            PluginRegistry.addPluginType(StepPluginType.getInstance());
            PluginRegistry.addPluginType(PartitionerPluginType.getInstance());
            PluginRegistry.addPluginType(JobEntryPluginType.getInstance());
            PluginRegistry.addPluginType(RepositoryPluginType.getInstance());
            PluginRegistry.addPluginType(DatabasePluginType.getInstance());
            PluginRegistry.addPluginType(LifecyclePluginType.getInstance());
            PluginRegistry.addPluginType(KettleLifecyclePluginType.getInstance());
            PluginRegistry.addPluginType(ImportRulePluginType.getInstance());
            PluginRegistry.addPluginType(CartePluginType.getInstance());
            PluginRegistry.init();
            KettleVariablesList.init();
            initLifecycleListeners();
            initialized = true;
        }

    }

    private static void initLifecycleListeners() throws KettleException {
        final KettleLifecycleSupport s = new KettleLifecycleSupport();
        s.onEnvironmentInit();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                try {
                    s.onEnvironmentShutdown();
                } catch (Exception var2) {
                    System.err.println(BaseMessages.getString(KettleInit.PKG, "LifecycleSupport.ErrorInvokingKettleEnvironmentShutdownListeners", new String[0]));
                    var2.printStackTrace();
                }

            }
        });
    }

    public static boolean isInitialized() {
        return initialized != null;
    }

    public void loadPluginRegistry() throws KettlePluginException {
    }

/*    public static void environmentInit() throws KettleException {
        if (Thread.currentThread().getContextClassLoader() == null) {
            Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
        }

        Map<?, ?> prop = Constant.readProperties();
        Variables variables = new Variables();
        Iterator var2 = prop.keySet().iterator();

        String variable;
        while(var2.hasNext()) {
            Object key = var2.next();
            String variable = (String)key;
            variable = variables.environmentSubstitute((String)prop.get(key));
            variables.setVariable(variable, variable);
        }

        String[] var6 = variables.listVariables();
        int var7 = var6.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            variable = var6[var8];
            System.setProperty(variable, variables.getVariable(variable));
        }

        System.getProperties().put("KETTLE_HOME", Constant.KETTLE_HOME);
        System.getProperties().put("KETTLE_PLUGIN_BASE_FOLDERS", Constant.KETTLE_PLUGIN);
        System.getProperties().put("KETTLE_JS_HOME", Constant.KETTLE_SCRIPT);
        System.getProperties().put("Internal.Cluster.Size", "1");
        System.getProperties().put("Internal.Slave.Transformation.Number", "0");
        System.getProperties().put("Internal.Slave.Server.Name", "slave-trans-name");
        System.getProperties().put("Internal.Step.CopyNr", "0");
        System.getProperties().put("Internal.Step.Name", "step-name");
        System.getProperties().put("Internal.Step.Partition.ID", "partition-id");
        System.getProperties().put("Internal.Step.Partition.Number", "0");
        System.getProperties().put("Internal.Step.Unique.Count", "1");
        System.getProperties().put("Internal.Step.Unique.Number", "0");
    }*/
}
